<template>
  <div>
    <!-- -->
    <el-container>
      <el-header></el-header>
      <!--主体内容-->
      <el-main>
        <div class="login-contain">
          <el-row type="flex" justify="center">
            <el-col :span="5">
              <el-card>
                <div slot="header" class="card-head">
                  <span>系统登录</span>
                </div>
                <el-form
                  :model="adminUser"
                  :rules="rules"
                  size="small"
                  label-width="100px"
                  ref="adminUser"
                >
                  <el-form-item label="登录账号" prop="username">
                    <el-input
                      placeholder="请输入内容"
                      prefix-icon="el-icon-user"
                      v-model="adminUser.username"
                    >
                    </el-input>
                  </el-form-item>
                  <el-form-item label="登录密码" prop="password">
                    <el-input
                      placeholder="请输入内容"
                      prefix-icon="el-icon-lock"
                      v-model="adminUser.password"
                    >
                    </el-input>
                  </el-form-item>
                  <div class="login-btn">
                    <el-form-item>
                      <el-button type="primary" @click="login">登录</el-button>
                      <el-button @click="resetForm('adminUser')"
                        >重置</el-button
                      >
                    </el-form-item>
                  </div>
                </el-form>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-main>
      <el-footer></el-footer>
    </el-container>
  </div>
</template>

<script>
import Mock from "mockjs";
import Cookie from "js-cookie";
import { LoginByPhoneAndPwd } from "network/Home.js";
export default {
  name: "VueSysmangerLogin",

  data() {
    return {
      adminUser: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
      },
    };
  },

  mounted() {},

  methods: {
    login() {
      //   let token = Mock.Random.guid();
      //   Cookie.set("token", token);
      this.$refs.adminUser.validate((validate) => {
        if (validate) {
          LoginByPhoneAndPwd(
            this.adminUser.username,
            this.adminUser.password
          ).then((data) => {
            if ((data.stateCode = 200)) {
              Cookie.set("token", data.token);
              this.$router.push("/home");
              this.$message(data.message);
            } else {
              this.$message.error(data.message);
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
.login-contain {
  margin-top: 10%;
}
.card-head {
  font-size: 20px;
}
.login-btn {
  margin-left: 20px;
}
</style>